var logcat_apps;
var logcat_sentTime;
var logcat_knownProcesses = [];
logcat_knownProcesses.all = true;
var logcat_selected = [];
logcat_selected.all = true;
var logcat_lock = false;
var logcat_paused = false;
var logcat_lastRow;
var logcat_content;
var logcat_sFilter;
var logcat_tab;
var logcat_table;
var logcat_bPause;
var logcat_bResume;
var logcat_bFilter;
var logcat_filterDiv;
var logcat_trHead;
var logcat_rowsNum = 0;
var logcat_maxRows = 100;

function logcat_init() {
	logcat_content = document.getElementById("logcat_content");
	logcat_tab = main_createTab("logcat_content", _T("Logcat"));
	main_contents.push(logcat_content);
	// Toolbar
	logcat_bFilter = main_createTool(_T("FilterProcess"),
			"popup_show('Select processes',logcat_filterDiv,'logcat_filterdiv')");
	logcat_bPause = main_createTool(_T("Pause"), "logcat_pause();");
	logcat_bResume = main_createTool(_T("Resume"), "logcat_resume();");
	logcat_bResume.style.display = "none";
	// Filter popup
	logcat_filterDiv = document.createElement("p");
	logcat_sFilter = document.createElement("select");
	logcat_sFilter.setAttribute("multiple", true);
	logcat_sFilter.setAttribute("onChange", "logcat_setFilter();");
	var option = document.createElement("option");
	option.setAttribute("value", "all");
	option.appendChild(document.createTextNode(_T("All")));
	logcat_sFilter.appendChild(option);
	logcat_filterDiv.appendChild(logcat_sFilter);
	// Table head
	logcat_table = document.createElement("table");
	logcat_trHead = document.createElement("tr");
	var th = document.createElement("th");
	th.appendChild(document.createTextNode(_T("LevelDot")));
	th.style.width = "1.2em";
	logcat_trHead.appendChild(th);
	th = document.createElement("th");
	th.appendChild(document.createTextNode(_T("Time")));
	th.style.width = "8em";
	logcat_trHead.appendChild(th);
	th = document.createElement("th");
	th.appendChild(document.createTextNode(_T("Process")));
	logcat_trHead.appendChild(th);
	th = document.createElement("th");
	th.appendChild(document.createTextNode(_T("Tag")));
	logcat_trHead.appendChild(th);
	th = document.createElement("th");
	th.appendChild(document.createTextNode(_T("Message")));
	logcat_trHead.appendChild(th);
	logcat_content.appendChild(logcat_table);
}

function logcat_onShow() {
	main_toolbar.appendChild(logcat_bFilter);
	main_toolbar.appendChild(logcat_bPause);
	main_toolbar.appendChild(logcat_bResume);
}

function logcat_readLogcat() {
	if (logcat_lock || logcat_paused) {
		return;
	}
	logcat_lock = true;
	var req = new XMLHttpRequest();
	req.timeout = 5000;
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200 && !logcat_paused) {
			if (req.responseText.length < 1) {
				return;
			}
			var response = main_readJSONResponse(req);
			if(!response){
				return;
			}
			addPing(new Date().getTime() - logcat_sentTime);
			logcat_apps = response.apps;
			for (pid in logcat_apps) {
				if (!logcat_knownProcesses[logcat_apps[pid]]) {
					logcat_knownProcesses[logcat_apps[pid]] = true;
					var newProcess = document.createElement("option");
					newProcess.setAttribute("value", logcat_apps[pid]);
					newProcess.appendChild(document
							.createTextNode(logcat_apps[pid]));
					logcat_sFilter.appendChild(newProcess);
					logcat_sFilter.setAttribute("size", 10);
				}
			}
			logcat_content.removeChild(logcat_table);
			logcat_table = document.createElement("table");
			logcat_table.appendChild(logcat_trHead);
			logcat_lastRow = null;
			var logs = response.logs;
			if (logs != null) {
				logcat_rowsNum = 0;
				for ( var i = logs.length - 3; logcat_rowsNum < logcat_maxRows
						&& i >= 0; i--) {
					if (logs[i].length < 1) {
						var header = logs[i + 1].split(" ");
						var message = logs[i + 2];
						logcat_writeRowDown(header, message);
						i -= 2;
					}

				}
			}
			logcat_content.appendChild(logcat_table);
		}
		logcat_lock = false;
	};
	main_encryptReq(req,"logcat");
	logcat_sentTime = new Date().getTime();
}

function logcat_writeRowDown(_header, _message) {
	// alert("'"+_header+"' '"+_message+"'");
	var time;
	var pidPod;
	var pid;
	var levelTag;
	var level;
	var tag;
	var fieldIndex = 0;
	var fieldValue;
	for ( var i = 1; i < _header.length; i++) {
		fieldValue = _header[i];
		if (fieldValue.length > 2) {
			// alert(fieldIndex+": "+fieldValue);
			switch (fieldIndex) {
			case 1:
				time = fieldValue;
				break;
			case 2:
				pidPod = fieldValue.split(":");
				pid = pidPod[0];
				break;
			case 3:
				levelTag = fieldValue.split("/");
				level = levelTag[0];
				tag = levelTag[1];
				break;
			}
			fieldIndex++;
		}
	}
	var processNS = logcat_apps[pid];
	if (logcat_selected.all || logcat_selected[processNS]) {
		var process;
		if (processNS) {
			process = processNS.substring(processNS.lastIndexOf(".") + 1);
		} else {
			process = "PID "+ pid;
		}
		var tr = document.createElement("tr");
		tr.className = level;
		var tdLevel = document.createElement("td");
		tdLevel.appendChild(document.createTextNode(level));
		var tdTime = document.createElement("td");
		tdTime.appendChild(document.createTextNode(time));
		var tdProcess = document.createElement("td");
		tdProcess.appendChild(document.createTextNode(process));
		tdProcess.title = processNS;
		var tdTag = document.createElement("td");
		tdTag.appendChild(document.createTextNode(tag));
		var tdMessage = document.createElement("td");
		tdMessage.appendChild(document.createTextNode(_message));
		tr.appendChild(tdLevel);
		tr.appendChild(tdTime);
		tr.appendChild(tdProcess);
		tr.appendChild(tdTag);
		tr.appendChild(tdMessage);
		logcat_table.appendChild(tr);
		logcat_rowsNum++;
	}
}

function logcat_setFilter() {
	for ( var i = 0; i < logcat_sFilter.length; i++) {
		if (logcat_sFilter[i].selected) {
			logcat_selected[logcat_sFilter[i].value] = true;
		} else {
			logcat_selected[logcat_sFilter[i].value] = false;
		}
	}
}

function logcat_toggleFilter() {
	if (logcat_filterDiv.style.display != "block") {
		logcat_filterDiv.style.display = "block";
	} else {
		logcat_filterDiv.style.display = "none";
	}
}

function logcat_pause() {
	logcat_bResume.style.display = "inline-block";
	logcat_bPause.style.display = "none";
	logcat_paused = true;
}

function logcat_resume() {
	logcat_bPause.style.display = "inline-block";
	logcat_bResume.style.display = "none";
	logcat_paused = false;
}
